﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Enumerations;

public partial class Master_AdminOnlinAddPoint : Admin_BasePage
{
    private int id = 0;
    protected AddPoint_Model model = new AddPoint_Model();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((id = StringHelper.isNumber_int(Request["id"])) != 0)
            {
                ViewState["id"] = id;
                //model=AddPoint_BLL.CreateNew.GetModel(this.id);
                BindUsers();
            }
            else
            {
                Response.Redirect("Admin_Login.aspx");
            }
        }
        else
        {
            if (ViewState["id"]!=null)
            {
                id = StringHelper.isNumber_int(ViewState["id"].ToString());
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        AddPoint();
        ResultStr("Admin_AddPoint.Aspx", "充值成功!");
    }

    public void AddPoint()
    {
        model = AddPoint_BLL.CreateNew.GetModel(this.id);
        Point_Log_Model Point_Logmodel = new Point_Log_Model();
        Point_Logmodel.NetIp = StringHelper.Ip();
        Point_Logmodel.Point = -(model.Point);
        Point_Logmodel.Readme = "管理员用充值卡充值点数:" + Point_Logmodel.Point + "点";
        string sqlText = "update AddPoint set UseType={0},State=1,UseId={1},UseTime='{2}' where id={3}";
        if (ddluserId.Items.Count == 0)
        {
            return;
        }
        else
        {
            if (ddluserId.SelectedValue=="0")
            {
                Response.Write("<script>alert('请选择要充值的用户');window.location.href='AdminOnlinAddPoint.aspx?id="+this.id+"';</script>");
            }
        }
        Point_Logmodel.UserId = StringHelper.isNumber_int(ddluserId.SelectedValue);
        int userType=StringHelper.isNumber_int(ddlUserType.SelectedValue);
        Point_Logmodel.UserType = userType;
        Point_Logmodel.Addtime = DateTime.Now;
        sqlText = string.Format(sqlText, userType, Point_Logmodel.UserId, DateTime.Now, this.id);
        
        
        int res = ExcuteSqlTextBLL.ExcuteResult(sqlText);

        Point_Log_Bll.CreateNew.Add(Point_Logmodel);
       
    }

    public void BindUsers()
    {
        if (StringHelper.isNumber_int(ddlUserType.SelectedValue) == (int)UserTypeEnum.Agent)
        {
            DataTable dataTable = Agen_BLL.CreateNew.GetDataTable("");
            ddluserId.DataSource = dataTable;
            ddluserId.DataTextField = "AgentUserName";
            ddluserId.DataValueField = "Id";
            ddluserId.DataBind();
            
        }
        else if (StringHelper.isNumber_int(ddlUserType.SelectedValue) == (int)UserTypeEnum.User)
        {
            DataTable dataTable1 = User_Bll.CreateNew.GetDataTable();
            ddluserId.DataSource = dataTable1;
            ddluserId.DataTextField = "UserName";
            ddluserId.DataValueField = "Userid";
            ddluserId.DataBind();
        }
        ddluserId.Items.Insert(0, new ListItem("请选择要充值的用户", "0"));
        
    }
    protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindUsers();
    }
}
